import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/commom/add_friend/search_add_friend_view.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/util/rong_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/null_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:get/get.dart';

import 'new_friend_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2022/1/19 10:54
/// filePath： lib/page/rong_im/commom/new_friend/new_friend_view.dart
/// desc: 新好友添加页面
/// <summary>
///
class NewFriendPage extends StatefulWidget {
  @override
  _NewFriendPageState createState() => _NewFriendPageState();
}

class _NewFriendPageState extends State<NewFriendPage> {
  final logic = Get.put(NewFriendLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getDATA();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarWidget(title: "新的好友"),
      backgroundColor: Colors.white,
      body: GetBuilder<NewFriendLogic>(
        builder: (controller) {
          return Container(
            child: Column(
              children: [
                rowBaseWidget(
                    padding: EdgeInsets.all(15),
                    leftText: "添加手机号联系人",
                    onTap: (){
                      Get.to(() => SearchAddFriendPage(),preventDuplicates: false);
                    }
                ),
                if (logic.data == null || logic.data!.length == 0) nullCenterWidget(
                    msg: "暂无新好友~"
                ) else Expanded(
                  child:  EasyRefresh(
                    header: MaterialHeader(
                    ),
                    onRefresh: () async{
                      logic.getDATA();
                    },
                    child: ListView.separated(
                      physics: BouncingScrollPhysics(),
                      padding: EdgeInsets.symmetric(vertical: 15),
                      shrinkWrap: true,
                      itemBuilder: (ctx,index){
                        return ListTile(
                          title: Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Expanded(child: RongWidget.title("${logic.data![index].friendName} 申请添加好友",maxLines: 2),),
                              ],
                            ),
                          ),
                          subtitle: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              RongWidget.text("${logic.data![index].createTime}"),
                              rightButton(index),
                            ],
                          ),
                          leading:  Image.network("${logic.data![index].picUrl}",width: 50,height: 50,fit: BoxFit.cover,),
                        );
                      },
                      itemCount: logic.data!.length,
                      separatorBuilder: (BuildContext context, int index) {
                        return Divider(color: Colors.grey.shade300,);
                      },
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<NewFriendLogic>();
    super.dispose();
  }

  // 0-申请，1-通过，2-拒绝
  rightButton(index) {
    if(logic.data![index].friendType == 0){
      return Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            SizedBox(
              width: 60,
              height: 30,
              child: RongWidget.gradientButton("接受",onTap:(){
                logic.friendApplyOperate(1,logic.data![index].friendId);
              },color: Colors.green,),
            ),
            SizedBox(width: 10,),
            SizedBox(
              width: 60,
              height: 30,
              child: RongWidget.gradientButton("拒绝",color: Colors.redAccent,onTap:(){
                logic.friendApplyOperate(2,logic.data![index].friendId);
              },),
            ),
          ],
        ),
      );
    }
    if(logic.data![index].friendType == 1){
      return RongWidget.text("已成为好友");
    }
    if(logic.data![index].friendType == 2){
      return RongWidget.text("已被拒绝",color: Colors.redAccent);
    }
    return SizedBox();
  }
}